package com.sky.lightleafpicturepavilionserver.controller;

import com.sky.lightleafpicturepavilionserver.common.Result;
import com.sky.lightleafpicturepavilionserver.dto.SystemConfigDTO;
import com.sky.lightleafpicturepavilionserver.dto.SystemConfigUpdateDTO;
import com.sky.lightleafpicturepavilionserver.service.SystemConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 系统配置控制器
 */
@Tag(name = "系统配置管理", description = "小程序系统配置相关接口")
@Validated
@RestController
@RequestMapping("/api/v1/systemConfig")
@Slf4j
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;

    /**
     * 获取所有系统配置
     */
    @Operation(summary = "获取所有系统配置", description = "获取小程序所有系统配置信息")
    @GetMapping("/list")
    public Result<List<SystemConfigDTO>> getAllConfigs() {
        log.info("请求获取所有系统配置");

        try {
            List<SystemConfigDTO> configList = systemConfigService.getAllConfigs();
            return Result.success("获取系统配置成功", configList);
        } catch (Exception e) {
            log.error("获取系统配置失败", e);
            return Result.error("获取系统配置失败");
        }
    }

    /**
     * 根据配置键获取配置值
     */
    @Operation(summary = "获取指定配置", description = "根据配置键获取指定的配置值")
    @GetMapping("/value/{configKey}")
    public Result<String> getConfigValue(@PathVariable String configKey) {
        log.info("请求获取配置值，configKey: {}", configKey);

        try {
            String configValue = systemConfigService.getConfigValue(configKey);
            if (configValue != null) {
                return Result.success("获取配置值成功", configValue);
            } else {
                return Result.error("配置不存在");
            }
        } catch (Exception e) {
            log.error("获取配置值失败，configKey: {}", configKey, e);
            return Result.error("获取配置值失败");
        }
    }

    /**
     * 更新系统配置
     */
    @Operation(summary = "更新系统配置", description = "更新指定的系统配置")
    @PutMapping("/update")
    public Result<String> updateConfig(@Valid @RequestBody SystemConfigUpdateDTO updateDTO) {
        log.info("请求更新系统配置，configKey: {}", updateDTO.getConfigKey());

        try {
            Boolean success = systemConfigService.updateConfig(updateDTO);
            if (success) {
                return Result.success("更新配置成功");
            } else {
                return Result.error("更新配置失败");
            }
        } catch (Exception e) {
            log.error("更新系统配置失败", e);
            return Result.error("更新配置失败");
        }
    }

    /**
     * 批量更新系统配置
     */
    @Operation(summary = "批量更新系统配置", description = "批量更新多个系统配置")
    @PutMapping("/batchUpdate")
    public Result<String> batchUpdateConfigs(@Valid @RequestBody List<SystemConfigUpdateDTO> updateList) {
        log.info("请求批量更新系统配置，数量: {}", updateList.size());

        try {
            Boolean success = systemConfigService.batchUpdateConfigs(updateList);
            if (success) {
                return Result.success("批量更新配置成功");
            } else {
                return Result.error("批量更新配置失败");
            }
        } catch (Exception e) {
            log.error("批量更新系统配置失败", e);
            return Result.error("批量更新配置失败");
        }
    }

    /**
     * 初始化默认配置
     */
    @Operation(summary = "初始化默认配置", description = "初始化系统默认配置（仅在配置不存在时创建）")
    @PostMapping("/initDefault")
    public Result<String> initDefaultConfigs() {
        log.info("请求初始化默认配置");

        try {
            systemConfigService.initDefaultConfigs();
            return Result.success("初始化默认配置成功");
        } catch (Exception e) {
            log.error("初始化默认配置失败", e);
            return Result.error("初始化默认配置失败");
        }
    }
}